package com.liuzy.executor;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * MySql执行者
 * 
 * @author liuzy
 * @version 2015年5月2日
 */
public class MySqler extends Executor implements IExecutor {

	private static String driver = "com.mysql.jdbc.Driver";
	private String url = "jdbc:mysql://localhost:3306/";
	private String username = "root";
	private String password = "111111";
	private Connection conn;
	private Statement stmt;

	public MySqler(InputStream in, OutputStream out) {
		this.in = in;
		this.out = out;
		this.init();
	}

	public MySqler(InputStream in, OutputStream out, String url, String username, String password) {
		this.in = in;
		this.out = out;
		if (null != url && !"".equals(url) && url.indexOf(":") > 0) {
			this.url = "jdbc:mysql://" + url;
		}
		if (null != username && !"".equals(username)) {
			this.username = username;
		}
		if (null != password && !"".equals(password)) {
			this.password = password;
		}
		this.init();
	}

	@Override
	public void init() {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, username, password);
			stmt = conn.createStatement();
			this.writeMsg("**************\r\nmysql已连接!\r\n退出请输入exit回车.\r\nsql>");
		} catch (Exception e) {
			this.writeMsg(e.getMessage() + "\r\n");
			e.printStackTrace();
			this.execute("exit a");
		}
	}

	@Override
	public void execute(String line) {
		if (!"".equals(line)) {
			String[] sql = (line + " ").split(" ");
			String first = sql.length > 0 ? sql[0] : line;
			String second = sql.length > 1 ? sql[1] : "";
			if ("exit".equals(first.toLowerCase())) {
				System.out.println("sdasssssss--------------");
				return;
			}
			if (null != second && !"".equals(second))
				switch (first.toUpperCase()) {
				case "SELECT":
				case "UPDATE":
				case "SHOW":
					this.query(line);
					break;
				case "USE":
				case "CREATE":
				case "INSERT":
				case "DELETE":
				case "DROP":
					this.update(line);
					break;
				default:
					this.writeMsg("这是神马?\r\nsql>");
					break;
			}
		} else {
			this.writeMsg("sql>");
		}
	}

	@Override
	public void destroy() {
		try {
			conn.close();
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void query(String sql) {
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			this.writeMsg(e.getMessage() + "\r\nsql>");
			return;
		}
		try {
			while (rs.next()) {
				for (int i = 1; i < 100; i++) {
					try {
						this.writeMsg(rs.getString(i) + "\t");
					} catch (Exception e) {
						continue;
					}
				}
				this.writeMsg("\r\n");
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.writeMsg("sql>");
	}

	public void update(String sql) {
		int count;
		try {
			count = stmt.executeUpdate(sql);
			if (count > 0) {
				this.writeMsg(count + "行已更新\r\nsql>");
			} else {
				this.writeMsg("成功\r\nsql>");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
